package cn.tianjindong.blog_manager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tianjindong.blog.pojo.BlogArticleCategory;
import cn.tianjindong.blog_manager.service.CategoryService;

/**
 * 博客系统文章分类管理Controller
 * 
 * @author 田 金 东
 *
 */
@Controller
public class CategoryController {

	@Autowired
	private CategoryService categoryService;

	/**
	 * 跳转到文章分类页面
	 * 
	 * @return
	 */
	@RequestMapping("/category.html")
	public String toCategoryPage(Model model) {
		List<BlogArticleCategory> categoryList = categoryService.getAllCategory();
		model.addAttribute("categoryList", categoryList);
		return "/category";
	}

	/**
	 * 分类添加
	 * 
	 * @param category
	 * @return
	 */
	@RequestMapping("/category/add.action")
	public String addCategory(BlogArticleCategory category) {
		categoryService.addArticleCategory(category);
		return "redirect:/category.html";
	}

	/**
	 * 前往分类修改页面
	 * 
	 * @return
	 */
	@RequestMapping("/category/update/{id}.html")
	public String toUpdateCategoryPage(Model model, @PathVariable Long id) {
		BlogArticleCategory category = categoryService.getCategoryById(id);
		model.addAttribute("category", category);
		return "/update-category";
	}

	/**
	 * 更新分类信息
	 * 
	 * @param category
	 * @return
	 */
	@RequestMapping("/category/update.action")
	public String updateCategory(BlogArticleCategory category) {
		categoryService.updateCategory(category);
		return "redirect:/category.html";
	}

	/**
	 * 删除分类(Ajax请求)
	 * @param id
	 * @return
	 */
	@RequestMapping("/category/delete/{id}.action")
	public @ResponseBody String deleteCategory(@PathVariable Long id) {
		categoryService.deleteCategory(id);
		return null;
	}

}
